package com.ruoyi.wx.service.serviceImpl;

import com.ruoyi.wx.domain.Employee;
import com.ruoyi.wx.domain.vo.EmployeeVo1;
import com.ruoyi.wx.mapper.EmployeeMapper;
import com.ruoyi.wx.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Employee getUserInfo(Long userId) {
        return employeeMapper.selectById(userId);
    }

    @Override
    public Map<String, Object> getEmployeeApproval(Long employeeId) {
        Map<String, Object> map = new HashMap<>();
        List<EmployeeVo1> employeeApproval = employeeMapper.getEmployeeApproval(employeeId);

        // 初始化三个列表
        List<Map<String, Object>> allList = new ArrayList<>();
        List<Map<String, Object>> pendingList = new ArrayList<>();
        List<Map<String, Object>> approvedOrRejectedList = new ArrayList<>();

        // 处理数据并按状态分组
        for (int i = 0; i < employeeApproval.size(); i++) {
            EmployeeVo1 vo = employeeApproval.get(i);
            Map<String, Object> item = new HashMap<>();

            // 设置基本信息
            item.put("id", i + 1); // 使用索引作为ID
            item.put("applyTime", vo.getApplyTime());
            item.put("applicant", vo.getApplicant());
            item.put("project", vo.getProject());
            item.put("reason", vo.getReason());
            item.put("content", vo.getContent());
            item.put("currentApprover", vo.getCurrentApprover());

            // 根据status设置审批状态相关参数
            if ("pending".equals(vo.getStatus())) {
                item.put("approvalStatus", "审批中");
                item.put("statusType", "warning");
                item.put("statusIcon", "clock-o");
                item.put("statusColor", "#ff9900");
                item.put("progressText", "等待" + vo.getCurrentApprover() + "审核");
                pendingList.add(item);
            } else if ("approved".equals(vo.getStatus())) {
                item.put("approvalStatus", "已通过");
                item.put("statusType", "success");
                item.put("statusIcon", "success");
                item.put("statusColor", "#67c23a");
                item.put("resultIcon", "success");
                item.put("resultColor", "#67c23a");
                item.put("progressText", "已完成所有审批流程");
                approvedOrRejectedList.add(item);
            } else if ("rejected".equals(vo.getStatus())) {
                item.put("approvalStatus", "已拒绝");
                item.put("statusType", "danger");
                item.put("statusIcon", "cross");
                item.put("statusColor", "#f56c6c");
                item.put("progressText", "审批被拒绝，需重新提交");
                approvedOrRejectedList.add(item);
            }

            // 所有记录都添加到allList
            allList.add(item);
        }

        // 将处理后的数据放入map
        map.put("myApplyList", allList);
        map.put("pendingApprovalList", pendingList);
        map.put("approvedList", approvedOrRejectedList);

        System.out.println("全部数量: " + allList.size());
        System.out.println("待审批数量: " + pendingList.size());
        System.out.println("已审批数量: " + approvedOrRejectedList.size());
        System.out.println(map);

        return map;
    }
}
